package struts;

import item.Item;
import item.exception.ItemInvalidoException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import util.CampoObrigatorioException;
import util.ErroAcessoRepositorioException;

import fachada.Fachada;

public class ItemDispatchAction extends DispatchAction {
	public ActionForward listar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("listar");
	}
	
	public ActionForward formItem(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("formItem");
	}
	
	public ActionForward formAlterar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		return mapping.findForward("formAlterar");
	}
	
	public ActionForward alterar(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		int codigo = Integer.parseInt(request.getParameter("codigo"));
		String descricao = request.getParameter("descricao");
		double preco = Double.parseDouble(request.getParameter("preco"));
		try {
			Fachada.obterInstancia().atualizarItem(new Item(codigo,descricao,preco));
		} catch (ItemInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		}
		return mapping.findForward("listar");
	}
	
	public ActionForward remover(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		try {
			Fachada.obterInstancia().excluirItem(Integer.parseInt(request.getParameter("codigo")));
		} catch (NumberFormatException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ItemInvalidoException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		} 
		return mapping.findForward("listar");
	}
	
	public ActionForward inserir(ActionMapping mapping,ActionForm form, 
			HttpServletRequest request,	HttpServletResponse response){
		String descricao = request.getParameter("descricao");
		double preco = Double.parseDouble(request.getParameter("preco"));
		try {
			Fachada.obterInstancia().inserirItem(new Item(1,descricao,preco));
		} catch (ErroAcessoRepositorioException e) {
			request.setAttribute("mensagem", e.getMessage()+", "+e.getExcecaoInterna().getMessage());
			return mapping.findForward("erro");
		} catch (CampoObrigatorioException e) {
			request.setAttribute("mensagem", e.getMessage());
			return mapping.findForward("erro");
		}
		return mapping.findForward("listar");
	}
}
